﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class ucIE : UserControl
    {
        IEManager manager = new IEManager();
        DataTable MyIETable = null;

        void LoadData(DataTable myData)
        {
            dgvListofSupplier.DataSource = myData;
            for (int i = 0; i < dgvListofSupplier.Columns.Count; i++)
            {
                dgvListofSupplier.Columns[i].DataPropertyName = dgvListofSupplier.Columns[i].Name;
            }
        }
        private IE GetRow()
        {
            IE entity = new IE();
            entity.IEID = DataUtil.ToInt(dgvListofSupplier.SelectedRows[0].Cells[0].Value);
            entity.EmployeeID = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[1].Value);
            entity.IEDate = DataUtil.ToDate(dgvListofSupplier.SelectedRows[0].Cells[2].Value);
            entity.Reason = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[3].Value);
            entity.Amount = DataUtil.ToLong(dgvListofSupplier.SelectedRows[0].Cells[4].Value);
            return entity;
        }
        public ucIE()
        {
            InitializeComponent();
            dgvListofSupplier.AutoGenerateColumns = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmIE frm = new frmIE();
            frm.ShowDialog();
            if (frm.NewIE != null)
            {
                MyIETable.LoadDataRow(manager.CreateRow(frm.NewIE), false);
                LoadData(MyIETable);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvListofSupplier.SelectedRows.Count > 0)
            {
                frmIE frm = new frmIE(GetRow());
                frm.ShowDialog();
                if (frm.NewIE != null)
                {
                    MyIETable.Rows[dgvListofSupplier.SelectedRows[0].Index].ItemArray = manager.CreateRow(frm.NewIE);
                    LoadData(MyIETable);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvListofSupplier.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa không", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    IE entity = GetRow();
                    string result = manager.Delete(entity);
                    if (result == "Xóa thành công")
                    {
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MyIETable.Rows.RemoveAt(dgvListofSupplier.SelectedRows[0].Index);
                        LoadData(MyIETable);
                    }
                    else
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
            
        }

        private void biSearchByID_Click(object sender, EventArgs e)
        {
            frmSearchIE frm = new frmSearchIE(SearchIEType.ID);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void biSearchByName_Click(object sender, EventArgs e)
        {
            frmSearchIE frm = new frmSearchIE(SearchIEType.Reason);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MyIETable = manager.GetAll();
            LoadData(MyIETable);
        }

        private bool SetRole()
        {
            bool t = false;
            List<string> roles = AccountManager.GetRoleName();
            btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = false;
            btnSearch.Enabled =  false;
            if (roles.Contains("Thêm, xóa, cập nhật chi tiêu"))
            {
                btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = true;
                t = true;
            }
            if (roles.Contains("Tìm kiếm chi tiêu"))
            {
                btnSearch.Enabled =  true;
                t = true;
            }
            return t;
        }

        private void ucIE_Load(object sender, EventArgs e)
        {
            if (SetRole())
            {
                MyIETable = manager.GetAll();
                LoadData(MyIETable);
            }
        }
    }
}
